﻿namespace LiSAClient.WpfClient.ViewModel.Frame.Menus
{
    using System.Collections.Generic;
    using GalaSoft.MvvmLight;
    using LiSAClient.Core.Interfaces;
    using LiSAClient.Core.Viewmodel;

    public class DetachedWindowMenuViewModel : ContentViewModel
    {
        #region Observable Objects

        private readonly List<DetachedWindowMenuItemViewModel> menuItems;

        private bool hideMenus;

        public bool HideMenus
        {
            get
            {
                return this.hideMenus;
            }

            set
            {
                // sets and notifies of update if value has changed
                this.Set(() => this.HideMenus, ref this.hideMenus, value);
            }
        }

        private bool hideOptions;

        public bool HideOptions
        {
            get
            {
                return this.hideOptions;
            }

            set
            {
                // sets and notifies of update if value has changed
                this.Set(() => this.HideOptions, ref this.hideOptions, value);
            }
        }

        private DetachedWindowMenuItemViewModel closeWindowItem;

        public DetachedWindowMenuItemViewModel CloseWindowItem
        {
            get
            {
                return this.closeWindowItem;
            }

            private set
            {
                this.Set(() => this.CloseWindowItem, ref closeWindowItem, value);
            }
        }


        private DetachedWindowMenuItemViewModel commitAndCloseWindowItem;

        public DetachedWindowMenuItemViewModel CommitAndCloseWindowItem
        {
            get
            {
                return this.commitAndCloseWindowItem;
            }

            private set
            {
                this.Set(() => this.CommitAndCloseWindowItem, ref commitAndCloseWindowItem, value);
            }
        }

        #endregion

        public DetachedWindowMenuViewModel()
        {
            this.HideMenus = false;
            this.HideOptions = false;
            this.menuItems = new List<DetachedWindowMenuItemViewModel>();
            this.CloseWindowItem = new DetachedWindowMenuItemViewModel
            {
                IsVisible = true
            };

            this.CommitAndCloseWindowItem = new DetachedWindowMenuItemViewModel
            {
                IsVisible = false
            };

            this.menuItems.Add(CommitAndCloseWindowItem);
        }

        public void ButtonChecks(ViewModelBase content)
        {
            // Must be first, since it handles the relation between "Close" and "Close and Commit" buttons.
            this.CommitableHandle(content);

            this.HideOptions = this.menuItems.TrueForAll(x => !x.IsVisible);
        }

        private void CommitableHandle(ViewModelBase content)
        {
            var commitable = content as ICommitable;
            if (commitable != null)
            {
                this.CommitAndCloseWindowItem.IsVisible = true;
                this.CommitAndCloseWindowItem.ActionCommand = commitable.Commit;
                this.CloseWindowItem.IsVisible = false;
            }
            else
            {
                this.CommitAndCloseWindowItem.IsVisible = false;
                this.CommitAndCloseWindowItem.ActionCommand = null;
                this.CloseWindowItem.IsVisible = true;
            }
        }
    }
}
